package echat.future.cn.echat.my.view;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.util.Observable;
import java.util.Observer;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityZhuboWithdrawBinding;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.PlanList;
import echat.future.cn.echat.my.bean.WalletInfo;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.RxUtils;
import echat.future.cn.echat.my.utils.StatusBarUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

public class ZhuboWithdrawActivity extends BaseActivity<ActivityZhuboWithdrawBinding> implements Observer {

    @Override
    public int layoutId() {
        return R.layout.activity_zhubo_withdraw;
    }

    @Override
    public IBaseNavagation navagation() {
        return null;
    }

    @Override
    public void initView() {
        bind.navigationHeaderContainer.setOnReightClick(v -> startActivity(new Intent(this,WithdrawHistoryActivity.class)));
        show();
        UserHelper.getWithdrawAccount().addObserver(this);
        RxUtils.clickView(bind.confirm).subscribe(view ->withdraw());
        loadData();
        loadCon();
    }

    @Override
    public void update(Observable o, Object arg) {
        show();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.hideStatus(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        UserHelper.getWithdrawAccount().deleteObserver(this);
    }


    /**
     * 提现
     */
    private void withdraw(){
        if(bind.etMoney.getText().toString().equals("")){
            toast("请输入正确的提现金额");
            return;
        }

        try{
            int money =   Integer.parseInt(bind.etMoney.getText().toString());
        }catch (Exception e){
            toast("请输入正确的提现金额");
            return;
        }

        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).withdraw(UserHelper.getToken(),bind.etMoney.getText().toString()))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
                    toast("发起提现成功，请等待审核");
                    finish();
                })
                .start();
    }

    WalletInfo mwalletInfo;
    private void loadData(){
        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).getMoney(UserHelper.getToken()))
                .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<WalletInfo>() {
                    @Override
                    public void onSuccss(WalletInfo o) {
                        mwalletInfo = o;
                        bind.moneys.setText(mwalletInfo.getCanWithdraw()+"元");
                    }
                })
                .start();
    }


    private void show(){
        if(!UserHelper.getWithdrawAccount().getAlipay_account().equals("")) {
            bind.linearLayout3.setVisibility(View.VISIBLE);
            bind.noAccount.setVisibility(View.GONE);
            bind.account.setText(UserHelper.getWithdrawAccount().getAlipay_account());
            bind.bindPohone.setText(UserHelper.getWithdrawAccount().getMobile());
            bind.name.setText(UserHelper.getWithdrawAccount().getAlipay_username());
        }
    }


    private void loadCon(){
        new HttpUtilsRequst<PlanList>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).planList(UserHelper.getToken(),"1"))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<PlanList>) planList -> {
                    bind.numbers.setText(planList.getContribution_left());
                })
                .start();
    }
}
